package com.rainbow.support.xss.handler;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * XSS过滤
 * jackson 序列化工具
 * @author rainbow
 */
@Slf4j
public class JsonXssSerializer extends JsonSerializer<String> {

	private final JsonXssCleaner jsonXssCleaner;

	public JsonXssSerializer(JsonXssCleaner jsonXssCleaner) {
		this.jsonXssCleaner = jsonXssCleaner;
	}

	@Override
	public Class<String> handledType() {
		return String.class;
	}

	@Override
	public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
		if (value != null) {
			String str=jsonXssCleaner.unescape(value);
			jsonGenerator.writeString(str);
		}
	}

}
